## "Public Preferences for International Law Compliance: 
##  Respecting Legal Obligations or Conforming to Common Practices?"
##
##
## Reproduce: 
##      - Table 1
##
## Input:  
##      - data/data_cleaned.csv
##
## Output: 
##      - t1.csv
##
## Saki Kuzushima
## 01/06/2023

library(dplyr)
#library(xtable)
#library(reporttools)
#load("prefectures.rda")
data <- read.csv(file="data/data_cleaned.csv")

# Gender
gender <- addNA(factor(data$gender, levels=c(1,2,3), labels=c("Male", "Female", "Other")), ifany=T)
gender_count <- table(gender)
gender_prop <- round(prop.table(table(gender))*100, 1)

res_gender <- data.frame(variable="Gender",
                         levels=names(gender_count),
                         count=as.numeric(gender_count),
                         prop=as.numeric(gender_prop))

# Age
age_char <- case_when(data$age < 30 ~ "19-30",
                      data$age >=30 & data$age < 40 ~ "30-40",
                      data$age >=40 & data$age < 50 ~ "40-50",
                      data$age >=50 & data$age < 60 ~ "50-60",
                      data$age >=60 ~ "60-79")
age <- addNA(factor(age_char), ifany=T)
age_count <- table(age)
age_prop <- round(prop.table(table(age))*100, 1)

res_age <- data.frame(variable="Age",
                      levels=names(age_count),
                      count=as.numeric(age_count),
                      prop=as.numeric(age_prop))

# Education: College or not
educ_char <- case_when(data$educ < 6 ~ "Not Collage",
                       data$educ == 6 | data$educ == 7 ~ "Collage")
educ <- addNA(factor(educ_char), ifany=T)
educ_count <- table(educ)
educ_prop <- round(prop.table(table(educ))*100, 1)

res_educ <- data.frame(variable="Education",
                       levels=names(educ_count),
                       count=as.numeric(educ_count),
                       prop=as.numeric(educ_prop))

# Party
party_char  <- case_when(data$party_ldp == 1 ~ "LDP",
                         data$party_none == 1 ~ "None",
                         data$party_other == 1 ~ "Other")
party <- addNA(factor(party_char), ifany=T)
party_count <- table(party)
party_prop <- round(prop.table(table(party))*100, 1)

res_party <- data.frame(variable="Party",
                        levels=names(party_count),
                        count=as.numeric(party_count),
                        prop=as.numeric(party_prop))

# Income (yen)
income_labels <- c("< 2M", "2M-4M", "4M-6M", "6M-8M", "8M-10M", "10M-12M", "12M-15M", "15M >")
income <- addNA(factor(data$income, levels=seq(1,8), labels=income_labels))
income_count <- table(income)
income_prop <- round(prop.table(table(income))*100, 1)

res_income <- data.frame(variable="Income",
                         levels=names(income_count),
                         count=as.numeric(income_count),
                         prop=as.numeric(income_prop))

# Cosmopolitan
cosmp <- addNA(factor(data$id_cosmp, levels=seq(1,6), labels=seq(1,6)), ifany=T)
cosmp_count <- table(cosmp)
cosmp_prop <- round(prop.table(table(cosmp))*100, 1)

res_cosmp <- data.frame(variable="Cosmopolitan",
                        levels=names(cosmp_count),
                        count=as.numeric(cosmp_count),
                        prop=as.numeric(cosmp_prop))

# Combine all
res_all <- rbind(res_gender, res_age, res_educ, res_income, res_party, res_cosmp)
write.csv(res_all, file="results/main_si/t1.csv")
